//
//  DropDown1.m
//  pppppppppppppppp
//
//  Created by lanou on 15/11/2.
//  Copyright © 2015年 李伟伟. All rights reserved.
//

#import "DropDown.h"
@implementation DropDown

-(NSArray *)tableArray
{
    if (_tableArray == nil) {
        _tableArray = [NSArray array];
    }
    return _tableArray;
}

-(id)initWithFrame:(CGRect)frame
{
    self=[super initWithFrame:frame];
    if(self){
        _showList = NO; //默认不显示下拉框
        _tv = [[UITableView alloc] initWithFrame:CGRectMake(0, 30, frame.size.width, 0)];
        _tv.delegate = self;
        _tv.dataSource = self;
        _tv.separatorColor = [UIColor lightGrayColor];
        _tv.hidden = YES;
        _tv.bounces = NO;
        _tv.backgroundColor = [UIColor colorWithRed:0.859 green:0.463 blue:0.000 alpha:1.000];
        [self addSubview:_tv];
        
        _kindLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, 30)];
        [self addSubview:_kindLabel];
        
        _downBT = [UIButton buttonWithType:UIButtonTypeCustom];
        _downBT.frame = CGRectMake(frame.size.width-120, 0, 220, 30);
        [_downBT setImage:[[UIImage imageNamed:@"xialasanjiao"] imageWithRenderingMode:UIImageRenderingModeAutomatic]  forState:UIControlStateNormal];
        [_downBT addTarget:self action:@selector(dropdown) forControlEvents:UIControlEventAllTouchEvents];
        [self addSubview:_downBT];
    }
    return self;
}
-(void)dropdown{
    [_kindLabel resignFirstResponder];
    if (_showList) {//如果下拉框已显示，什么都不做
        return;
    }else {//如果下拉框尚未显示，则进行显示
    
        //把dropdownList放到前面，防止下拉框被别的控件遮住
//        [self.superview bringSubviewToFront:self];
        
        CGRect sf = self.frame;
        sf.size.height = 200;
        self.frame = sf;
        
        _tv.hidden = NO;
        _showList = YES;//显示下拉框
        
        CGRect frame = _tv.frame;
        frame.size.height = 170;
        _tv.frame = frame;

//        [UIView beginAnimations:@"ResizeForKeyBoard" context:nil];
//        [UIView setAnimationCurve:UIViewAnimationCurveLinear];
//        [UIView commitAnimations];
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.tableArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
    }
    
    cell.textLabel.text = [_tableArray objectAtIndex:[indexPath row]];
    cell.textLabel.font = [UIFont systemFontOfSize:16.0f];
    cell.textLabel.textColor = [UIColor whiteColor];
    cell.backgroundColor = [UIColor colorWithRed:0.859 green:0.463 blue:0.000 alpha:1.000];
//    cell.accessoryType = UITableViewCellAccessoryNone;
//    cell.selectionStyle = UITableViewCellSelectionStyleGray;
    
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 20;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    _kindLabel.text = [_tableArray objectAtIndex:[indexPath row]];
    _showList = NO;
    _tv.hidden = YES;
    
    CGRect sf = self.frame;
    sf.size.height = 30;
    self.frame = sf;
    CGRect frame = _tv.frame;
    frame.size.height = 0;
    _tv.frame = frame;
    
    _i = indexPath.row;
    
    if (_delegate && [_delegate respondsToSelector:@selector(didEndSelectedRow)]) {
        [_delegate didEndSelectedRow];
    }
}

//- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
//{
//    // Return YES for supported orientations
//    return (interfaceOrientation == UIInterfaceOrientationPortrait);
//}


@end
